<?php
/**
 * @author ykm 2016-01-19
 * @describe 短信接口公用设置
 */
class erpapi_sms_config extends erpapi_config
{
    #使用其他方法生成签名
    public $otherSign = array(
        SMS_USER_INFO,
        SMS_SERVER_TIME
    );
    /**
     * 应用级参数
     *
     * @param String $method 请求方法
     * @param Array $params 业务级请求参数
     * @return Array
     * @author 
     **/
    public function get_query_params($method, $params){
        if(in_array($method, $this->otherSign)) {
            return array();
        }
        $query_params = array(
            'sign_time' => time(),
            'sign_method' => 'md5',
        );
        return $query_params;
    }

    /**
     * 获取请求地址
     *
     * @param String $method 请求方法
     * @param Array $params 业务级请求参数
     * @param Boolean $realtime 同步|异步
     * @return String
     * @author 
     **/
    public function get_url($method, &$params, $realtime){
        switch($method) {
            case SMS_SERVER_TIME :
                $url = 'http://webapi.sms.shopex.cn/';
                break;
            case SMS_USER_INFO :
                $url = 'http://api.sms.shopex.cn/';
                break;
            case SMS_NEW_OAUTH:
                $url = 'http://openapi.shopex.cn' . $this->get_path($method);
                break;
            default :
                $url = 'http://openapi.ishopex.cn' . $this->get_path($method);
                if($params['sign']) {
                    $url .= '?client_id=' . $params['client_id'] . '&sign_time=' . $params['sign_time'] . '&sign_method=' . $params['sign_method'] . '&sign=' . $params['sign'];
                    unset($params['client_id'], $params['sign_time'], $params['sign_method'], $params['sign']);
                }
                break;
        }
        return $url;
    }

    /**
     * 签名
     *
     * @param Array $params 参数
     * @return String
     * @author 
     **/
    public function gen_sign(&$params,$method=''){
        if(in_array($method, $this->otherSign)) {
            return null;
        }
        $secret = $params['secret'];
        if(isset($params['secret'])) {
            unset($params['secret']);
        }
        $path = $this->get_path($method);
        $signUrl = $params['signUrl'];
        if(isset($params['signUrl'])) {
            unset($params['signUrl']);
        }
        $tmpParam = $params;
        if($signUrl) {
            $query[] = 'client_id=' . $tmpParam['client_id'];
            $query[] = 'sign_method=' . $tmpParam['sign_method'];
            $query[] = 'sign_time=' . $tmpParam['sign_time'];
            unset($tmpParam['client_id'], $tmpParam['sign_time'], $tmpParam['sign_method']);
            $strQuery = implode('&', $query);
        } else {
            $strQuery = '';
        }
        foreach($tmpParam as $k=>$v){
            $tmp[] = $k.'='.$v;
        }
        ksort($tmpParam);
        $tmp = array();
        foreach($tmpParam as $k=>$v){
            $tmp[] = $k.'='.$v;
        }
        $strParam = implode('&', $tmp);
        $sign = array(
            $secret,
            'POST',
            rawurlencode($path),
            null,
            rawurlencode($strQuery),
            rawurlencode($strParam),
            $secret
        );
        $sign = implode('&', $sign);
        return strtoupper(md5($sign));
    }

    protected function get_path($method) {
        $arr = explode('.', $method);
        if($arr[0] == 'erp' && $arr[1] == 'sms') {
            unset($arr[0], $arr[1]);
            return '/' . implode('/', $arr);
        }
        return '';
    }
}